//
//  SettingViewController.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/1.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "SettingViewController.h"
#import "MyBankViewController.h"
#import "ChangePasswordVC.h"
#import "HengYouCai-Swift.h"

@interface SettingViewController ()

@property (nonatomic, retain)NSArray *titleArr;
@end

@implementation SettingViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Normal_BackColor;
    self.title = @"个人设置";
    
    [self initData];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
- (void)initData{
    self.titleArr = @[@"",@"实名认证",@"我的银行卡",@"绑定手机",@"",@"修改登录密码",@"修改交易密码",@"投资风格",@"",@"退出登录"];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 10;
            break;
        case 4:
            return 10;
            break;
        case 8:
            return 50;
            break;
        default:
            return 44;
            break;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     

     UITableViewCell *cell = nil;
     if ([_titleArr[indexPath.row] isEqualToString:@""]) {
         
         // 空的
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
         cell.backgroundColor = [UIColor colorWithHexString:@"#f4f5f8"];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
         return cell;

     } else if (indexPath.row==3){
         
         // 绑定手机
         cell = [tableView dequeueReusableCellWithIdentifier:@"SettingNORightCellID" forIndexPath:indexPath];
         UILabel *titleLab = [cell viewWithTag:1001];
         titleLab.text = _titleArr[indexPath.row];
         UILabel *contentLab = [cell viewWithTag:1002];
         contentLab.textColor = [UIColor colorWithHexString:@"#666666"];
         
//         NSMutableString *accoutStr = [WLFileManager readUserDataForKey:kUserAccount]; 这样会出错 因为得到的还是不可变字符串
         NSString *account = [WLFileManager readUserDataForKey:kUserAccount];
         if (account.length>1) {
             NSMutableString *accoutStr = [NSMutableString stringWithString:account];
             [accoutStr replaceCharactersInRange:NSMakeRange(3, accoutStr.length-3-4) withString:@"****"];
             contentLab.text = accoutStr;
         }else{
             contentLab.textColor = Normal_RedColor;

             contentLab.text = @"未绑定";
         }
         
         return cell;

     }else if (indexPath.row==7){
         // 投资风格
         
         NSInteger userLevel = [[WLFileManager readUserDataForKey:kUserLevel] integerValue];

         cell = [tableView dequeueReusableCellWithIdentifier:@"SettingNOLineCellID" forIndexPath:indexPath];
         UILabel *titleLab = [cell viewWithTag:1001];
         titleLab.text = _titleArr[indexPath.row];
         UILabel *contentLab = [cell viewWithTag:1002];
         contentLab.textColor = userLevel>0?Normal_333Color:Normal_RedColor;

         switch (userLevel) {
             case 1:
                 contentLab.text = @"基础型";
                 break;
             case 2:
                 contentLab.text = @"保守型";
                 break;
             case 3:
                 contentLab.text = @"稳健型";
                 break;
             case 4:
                 contentLab.text = @"进取型";
                 break;
             default:
                 contentLab.text = @"去测评";
                 break;
         }
         return cell;

     }else if (indexPath.row==9){
         // 退出登录
         cell = [tableView dequeueReusableCellWithIdentifier:@"SettingLoginOutCellID" forIndexPath:indexPath];
         UILabel *titleLab = [cell viewWithTag:1001];
         titleLab.textColor = Normal_RedColor;
         titleLab.text = _titleArr[indexPath.row];
         return cell;

     }else{
         // 其他
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCellID" forIndexPath:indexPath];
         UILabel *titleLab = [cell viewWithTag:1001];
         titleLab.text = _titleArr[indexPath.row];
         UILabel *contentLab = [cell viewWithTag:1002];
         contentLab.textColor = [UIColor colorWithHexString:@"#666666"];
         
         switch (indexPath.row) {
             case 1:
             {
                 if (isRealName) {
                     NSString *name = [WLFileManager readUserDataForKey:kUserRealname];
                     name = [name stringByReplacingCharactersInRange:NSMakeRange(1, name.length-1) withString:@"**"];
                     contentLab.text = name;
                 }else{
                     contentLab.textColor = Normal_RedColor;

                     contentLab.text = @"未认证";
                 }
                 
             }
                 break;
             case 2:
             {
                 contentLab.text = isCard?@"已绑定":@"未绑定";
                 contentLab.textColor = isCard?Normal_333Color:Normal_RedColor;

             }
                 break;
             case 5:
             {
                 contentLab.text = @"";
             }
                 break;
             case 6:{
                 NSString *payWord = [WLFileManager readUserDataForKey:kUserPaypassword];
                 UILabel *contentLab = [cell viewWithTag:1002];
                 contentLab.textColor = payWord.length>2?Normal_333Color:Normal_RedColor;
                 contentLab.text = payWord.length>2?@"已设置":@"未设置";
                 return cell;
             }
                 break;
             default:
                 break;
         }
         
         return cell;

     }

 }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *userToken = [WLFileManager readUserDataForKey:kUserAccount];
    if (userToken.length<1) {
        // 未登录 就跳登录界面
        [self presentViewController:LogInVC animated:YES completion:^{
            [self.tableView reloadData];
        }];
    }else{
        
        if (indexPath.row == _titleArr.count-1) {
            [self loginOut];
        }else if (indexPath.row==2){
            
            // 实名认证与绑定银行卡成功 才可以跳转 我的银行卡 否则跳实名认证 并带过去基本用户信息 减少用户 填写操作
            UIViewController *VC = [[UIStoryboard storyboardWithName:@"Profile" bundle:nil] instantiateViewControllerWithIdentifier:@"MyBankViewControllerID"];

            NSString *realNameStatus = [WLFileManager readUserDataForKey:kRealnameStatus];
            NSString *cardStatus = [WLFileManager readUserDataForKey:kCardStatus];
            // 我的银行卡
            if ([realNameStatus isEqualToString:@"1"]&&[cardStatus isEqualToString:@"1"]) {
                [self.navigationController pushViewController:VC animated:YES withHideBottomBarWhenPush:YES pop:YES];

            }else{
                [self investPrepare];

            }
        }else if (indexPath.row==5||indexPath.row==6){
            NSString *payWord = [WLFileManager readUserDataForKey:kUserPaypassword];

            // 未实名// 或者 未设置交易密码 点击修改交易密码 弹框 否则 进入去修改界面
            if (indexPath.row==6&&(payWord.length<1)) {
                // 实名认证
                [self investPrepare];
                return;
            }
            // 修改交易密码

            UIViewController *setVC = [[UIStoryboard storyboardWithName:@"Profile" bundle:nil] instantiateViewControllerWithIdentifier:@"SetPasswordVCID"];
            
            
            ChangePasswordVC *VC = [[UIStoryboard storyboardWithName:@"Profile" bundle:nil] instantiateViewControllerWithIdentifier:@"ChangePasswordVCID"];
            VC.isLogin = indexPath.row==5;
            
            [self.navigationController pushViewController:indexPath.row==5?VC:payWord.length>2?VC:setVC animated:YES withHideBottomBarWhenPush:YES pop:YES];
        }else if (indexPath.row==1){
            // 实名认证
            UIViewController *VC = [[UIStoryboard storyboardWithName:@"Profile" bundle:nil] instantiateViewControllerWithIdentifier:@"CertificationViewControllerID"];
            
            NSString *userToken = [WLFileManager readUserDataForKey:kRealnameStatus];
            
            // 判断是否实名认证
            if ([userToken isEqualToString:@"1"]) {
                [self.navigationController pushViewController:VC animated:YES];

            }else{
                [self investPrepare];
            }
        }else if (7 == indexPath.row) {//投资风格
            
            NSInteger userLevel = [[WLFileManager readUserDataForKey:kUserLevel] integerValue];
            
            if (userLevel == 0) {//未设置跳转答题界面
                
                RiskAppraiseViewC *riskVC = [RiskAppraiseViewC new];
                [self.navigationController pushViewController:riskVC animated:YES];
                
            }else{///直接跳转展示结果页
                JCWRiskAssessmentResultVC * vc = [[JCWRiskAssessmentResultVC alloc] init];
                vc.typeIndex = userLevel;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }

    }
}
- (void)loginOut
{
    
    WLGetWeakSelf(weakSelf, self);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [weakSelf.view showHUDLoading];
        [HYCNetService loginOUtOnSuccess:^(NSDictionary *dictResult) {
            //
            [weakSelf.view hideHUDLoading:0];
            [WLFileManager removeUserDataForkey:kUserSessionId];
            [WLFileManager removeUserDataForkey:kCusCode];
            [WLFileManager removeUserDataForkey:kCardStatus];
            [WLFileManager removeUserDataForkey:kUserIdCard];
            [WLFileManager removeUserDataForkey:kUserRealname];
            [WLFileManager removeUserDataForkey:kUserPaypassword];
            [WLFileManager removeUserDataForkey:kRealnameStatus];
            [WLFileManager removeUserDataForkey:kUserLevel];
            [WLFileManager removeUserDataForkey:KWalletaward];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"isLoginOut" object:nil];
            // 信息保存之后 发通知

            [weakSelf.view hideHUDLoading:0];

            [self presentViewController:LogInVC animated:YES completion:^{
                [self.navigationController popToRootViewControllerAnimated:NO];
                UITabBarController *tab = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
                tab.selectedIndex = 0;
            }];
            
        } andFailed:^(NSInteger code, NSString *errorMsg) {
            [weakSelf.view showHUDFailTips:errorMsg hideDelay:1];
        }];
        
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
    NSString *str = @"退出登录后您将无法查看您的收益情况，要确认退出?";
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:str];
//    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#666666"] range:NSMakeRange(0, str.length)];
    // 段落样式
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    [paragraph setAlignment:NSTextAlignmentCenter];//设置对齐方式
    
    
    [alertControllerMessageStr addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#666666"],
                                               NSFontAttributeName:[UIFont systemFontOfSize:13],
                                               NSParagraphStyleAttributeName:paragraph} range:NSMakeRange(0, str.length)];
    
    [alert setValue:alertControllerMessageStr forKey:@"attributedMessage"];


//    unsigned int outCount = 0;
//    Ivar * ivars = class_copyIvarList([alert class], &outCount);
//    for (unsigned int i = 0; i < outCount; i ++) {
//        Ivar ivar = ivars[i];
//        const char * name = ivar_getName(ivar);
//        const char * type = ivar_getTypeEncoding(ivar);
//        NSLog(@"类型为 %s 的 %s ",type, name);
//    }
//    free(ivars);
}
- (void)investPrepare
{
  
        [[JCWAccountTrilogyAlertView alloc] initWithHeaderImageNamed:nil callBack:^{
            //
            InvestPrepareVC *prepare = [[InvestPrepareVC alloc] init];
            [self.navigationController pushViewController:prepare animated:YES];
        }];
    
}
@end
